﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Traxz.Models;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace Traxz.Controllers
{
    public class HomeController : Controller
    {
        TraxzRepository TraxzRepo = new TraxzRepository();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            TraxzRepo = new TraxzRepository();
            TraxzRepo.popTestData();
            return View();
        }

        [HttpPost]
        public ActionResult VerifyUser(LogOnModel model)
        {
           Boolean userDoesExit = (Membership.ValidateUser(model.UserName, model.Password));
           Guid userGuid = new Guid();
           
            //If user exists then return the guid to store on phone inorder to update stuff.
           if (userDoesExit)
           {
               userGuid = TraxzRepo.getUserGuid(model.UserName);
               var retInfo = Json(new { guid = userGuid});
               return retInfo;
           }
           else
           {
               var retInfo = Json(new { guid = "NOTFOUND" });
               return retInfo;
           }
        }
    
        [HttpPost]
        public ActionResult CreateUser (RegisterModel model)
        {

                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                    aspnet_Users user = TraxzRepo.getaUser(TraxzRepo.getUserGuid(model.UserName));
                    user.IsMale = model.IsMale;
                    user.Weight = Int32.Parse(model.Weight);
                    user.Age = Int32.Parse(model.Age);
                    var retInfo = Json(new { status = "Good" });
                    return retInfo;
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                    var retInfo = Json(new { error = ErrorCodeToString(createStatus) });
                    return retInfo;
                }
        }

    #region Status Codes
        private static string ErrorCodeToString(MembershipCreateStatus createStatus)
        {
            // See http://go.microsoft.com/fwlink/?LinkID=177550 for
            // a full list of status codes.
            switch (createStatus)
            {
                case MembershipCreateStatus.DuplicateUserName:
                    return "User name already exists. Please enter a different user name.";

                case MembershipCreateStatus.DuplicateEmail:
                    return "A user name for that e-mail address already exists. Please enter a different e-mail address.";

                case MembershipCreateStatus.InvalidPassword:
                    return "The password provided is invalid. Please enter a valid password value.";

                case MembershipCreateStatus.InvalidEmail:
                    return "The e-mail address provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidAnswer:
                    return "The password retrieval answer provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidQuestion:
                    return "The password retrieval question provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidUserName:
                    return "The user name provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.ProviderError:
                    return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                case MembershipCreateStatus.UserRejected:
                    return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                default:
                    return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
            }
        }
        #endregion
    }
    
}
